<!DOCTYPE html>
<html lang="en">
<head>
    <#include "/commonPage.html" />
    <script>
        $(function () {
            $('#editDevice').click(function () {
                var arr = $('#mydatagrid').datagrid('getChecked');
                if (arr.length == 0) {
                    $.messager.alert('提示', '请先选择一个要修改的用户!');
                    return;
                }
                if (arr.length > 1) {
                    $.messager.alert('提示', '只能选择一个要修改的用户!');
                    return;
                }
                var row = $('#mydatagrid').datagrid('getSelected');
                if (row) {
                    $('#win').window('open');
                    $('#ff').form('load', row);
                }
            })
        })
    </script>
</head>
<body>
<div><h1 style="color:#133ad5">管理用户</h1></div>
<div>
    <HR width="100%" color=#133ad5 SIZE=1>
</div>
<div id="cc" style="width:100%;height:100%">
    <table id="mydatagrid" class="easyui-datagrid" idField="Id" rownumbers="true" url="/user/list" pagination="true"
           striped="true"
           data-options="toolbar:'#tb',fitColumns:true,onLoadSuccess:function(){$('#mydatagrid').datagrid('clearChecked');}">
        <thead>
        <tr>
            <th field="uId" checkbox="true"></th>
            <th data-options="field:'userName',width:100">用户名称</th>
            <th data-options="field:'authority',width:100" formatter="showauthority">权限</th>
        </tr>
        </thead>
    </table>
</div>
<div id="tb">
    <div>
        <a id="editDevice" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改用户信息</a>
        <a id="deleteDevice" href="#" class="easyui-linkbutton" onclick="deleteDevice()"
           data-options="iconCls:'icon-cancel',plain:true">删除用户</a>
    </div>
    <div id="tbsearch" style="padding:3px">
        <span>用户名称:</span>
        <input id="userName2" style="line-height:26px;border:1px solid #ccc">
        <span>权限:</span>
        <select id="authority2" class="easyui-combobox" panelHeight="auto" required="true"
                style="width: 350px; margin: 0px; padding-top: 0px; padding-bottom: 0px; height: 28px; line-height: 28px;">
            <option value="1">员工</option>
            <option value="0">管理员</option>
        </select>
        <a href="#" class="easyui-linkbutton" plain="true" data-options="iconCls:'icon-search'"
           onclick="doSearch()">搜索</a>
    </div>
</div>
<div id="win" class="easyui-window" title="修改商户/用户" style="width:800px;height:600px;"
     data-options="iconCls:'icon-add',modal:true,minimizable:false,maximizable:false,collapsible:false,closed:true">
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'north',border:false" style="height:20px"></div>
        <div data-options="region:'center',border:false" style="padding: 10px;">
            <form id="ff" method="post" style="width: 500px;margin: 0 auto;">
                <input type="hidden" name="uId">
                <div style="margin-bottom:20px">
                    <input class="easyui-textbox" name="userName" style="width:100%" disabled="disabled"
                           data-options="label:'用户名称:',labelPosition:'left',labelWidth:100,prompt:'输入您用户的名称'">
                </div>
                <div style="margin-bottom:20px">
                    <input id="passWord" class="easyui-passwordbox" name="passWord" style="width:100%"
                           data-options="label:'账号密码:',labelPosition:'left',labelWidth:100,prompt:'输入账号密码/不输入则不改'">
                </div>
                <div style="margin-bottom:20px">
                    <input id="repassword" class="easyui-passwordbox" style="width:100%"
                           data-options="label:'重复账号密码:',labelPosition:'left',labelWidth:100,prompt:'输入账号密码/不输入则不改'">
                </div>
                <div style="margin-bottom:20px">
                    <label class="textbox-label textbox-label-left" for="mycombobox"
                           style="text-align: left; width: 100px; height: 30px; line-height: 30px;">权限级别:</label>
                    <span class="textbox textbox-invalid easyui-fluid" style="width: 350px;">
                    <select id="mycombobox" class="easyui-combobox" name="authority" panelHeight="auto" required="true"
                            style="width: 350px; margin: 0px; padding-top: 0px; padding-bottom: 0px; height: 28px; line-height: 28px;">
                        <option value="1">员工</option>
                        <option value="0">管理员</option>
                    </select>
                </span>
                </div>
            </form>
            <div style="width: 500px;margin: 30px auto 0 auto;" align="center">
                <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)"
                   onclick="updateBusinessfunction()" style="width:80px;margin-right: 50px;">修改</a>
            </div>
        </div>
    </div>
    <script>
        function updateBusinessfunction() {
            $('#ff').form('submit', {
                url: '/user/update',
                onSubmit: function () {
                    var isValid = $(this).form('validate');
                    if (!isValid) {
                        $.messager.progress('close');	// hide progress bar while the form is invalid
                    } else {
                        if ($('#repassword').val() != $('#passWord').val()) {
                            $.messager.alert('错误', '密码不一致', 'error');
                            return false;
                        }
                    }
                    return isValid;
                },
                success: function (data) {
                    if (data == "成功") {
                        $.messager.alert('Info', data, 'info');
                        $('#ff').form('clear');
                        $('#win').window("close");
                        $('#mydatagrid').datagrid("reload");
                    } else {
                        $.messager.alert('错误', data, 'error');
                    }
                }
            })
        };

        $('#city').combobox({
            onLoadSuccess: function () {
                // var data=$('#city').combobox('getData');
                // $('#city').combobox('select', data[0].id);
            }
        });
    </script>
</div>
</div>
<script>
    function doSearch() {
        var userName = $('#userName2').val();
        var authority = $('#authority2').val();
    }

    function showauthority(value, row, index) {
        if (row.authority == 1) {
            return "员工";
        } else {
            return "管理员";
        }
    }

    function getdatagridchecked() {
        var arr = $('#mydatagrid').datagrid('getSelections');
        var i = 0;
        var ids = [];
        for (i in arr) {
            ids.push(arr[i].uId);
        }
        ids = ids.join(",");
        return ids;
    }

    function deleteDevice() {
        ids = getdatagridchecked();
        if (ids.length == 0) {
            $.messager.alert('提示', '请先选择要删除的商户/用户!');
            return;
        }
        $.messager.confirm('确认', '确定删除选中的商户/用户吗？', function (r) {
            if (r) {
                $.post('/user/delete', {ids: ids}, function (data) {
                    if (data == "成功") {
                        $.messager.alert('提示', '删除商户/用户成功!', undefined, function () {
                            $("#mydatagrid").datagrid("reload");
                            $("#mydatagrid").datagrid("uncheckAll");
                        });
                    } else {
                        $.messager.alert('错误', data, 'error');
                    }
                })
            }
        });
    }

    function pagerFilter(data) {
        if (typeof data.length == 'number' && typeof data.splice == 'function') {	// is array
            data = {
                total: data.length,
                rows: data
            }
        }
        var dg = $(this);
        var opts = dg.datagrid('options');
        var pager = dg.datagrid('getPager');
        pager.pagination({
            onSelectPage: function (pageNum, pageSize) {
                opts.pageNumber = pageNum;
                opts.pageSize = pageSize;
                pager.pagination('refresh', {
                    pageNumber: pageNum,
                    pageSize: pageSize
                });
                dg.datagrid('loadData', data);
            }
        });
        if (!data.originalRows) {
            data.originalRows = (data.rows);
        }
        var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
        var end = start + parseInt(opts.pageSize);
        data.rows = (data.originalRows.slice(start, end));
        return data;
    }

    $(function () {
        $('#mydatagrid').datagrid({loadFilter: pagerFilter});
    });
</script>
</body>

</html>